package de.voichie.android.cipher.activities;

import de.voichie.android.cipher.R;
import de.voichie.android.cipher.business.Crypter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EncryptActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.encrypt_layout);
        
        
//      --- create encrypted password --------
        findViewById(R.id.btnStartEncryption).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                
            	// get the user input of password in clear form
            	EditText editTextClearPassword = (EditText) findViewById(R.id.editTextInputDeciphedPassword);
            	
            	if (editTextClearPassword.getText() == null || editTextClearPassword.getText().toString() == null || editTextClearPassword.getText().toString().equals(""))
            	{
            		// TODO message to user, that he has to enter a password in clear form
            		return;
            	}
            	String clearPassword = editTextClearPassword.getText().toString();
            	
            	// encrypt the password with business logic
            	String encryptedPassword = createEncryptedPassword(clearPassword);
            	
            	// display the encrypted password to the user
            	((EditText) findViewById(R.id.editTextOutputChiphedPassword)).setText(encryptedPassword);
            	((EditText) findViewById(R.id.editTextOutputChiphedPassword)).setVisibility(View.VISIBLE);
            	((TextView) findViewById(R.id.textChiphredPassword)).setVisibility(View.VISIBLE);
            }
        });
        
//      --- back is pressed ------------
      findViewById(R.id.btnBackToLastActivity).setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
          	onBackPressed();
          }
      });
        
    }
    
    private String createEncryptedPassword (String clearPassword) {
    	return Crypter.getCrypterSingleton().encryptPassword(clearPassword);
    }
}
